package com.znyj.servlet.mainController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.znyj.servlet.entity.ReportEntity;
import com.znyj.servlet.model.ReportQueryModel;
import com.znyj.servlet.service.ReportService;
import com.znyj.servlet.utilsTool.CommonTool;
import com.znyj.servlet.vo.Page;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by gaoliwei02 on 17/6/26.
 */
@Controller
@RequestMapping(value = "/report")
public class ReportController {
    private static final Logger logger = LoggerFactory.getLogger(ReportController.class);

    @Resource
    private ReportService reportService;

    @RequestMapping(value = "/getlist.inner", method = RequestMethod.POST)
    protected @ResponseBody String getListInner(@RequestBody String receiveData) throws ParseException, IOException {
        ReportQueryModel reportQueryModel = new ReportQueryModel(receiveData);//string到pojo
        Page<ReportEntity> page = reportService.createReportPage(reportQueryModel);//从数据库获取实体列表
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").disableHtmlEscaping().create();
        return gson.toJson(page);
    }

    //升级列表的API
    @RequestMapping(value = "/uplist.inner", method = RequestMethod.POST)
    protected @ResponseBody String upListApi(@RequestBody String receiveData, HttpSession session) throws ParseException, IOException {
        JSONObject getJson = JSON.parseObject(receiveData);
        JSONObject putJson = new JSONObject();
        List<String> deviceList = new ArrayList<String>();
        int deviceCount = 0;

        for (Map.Entry<String, Object> entry : getJson.entrySet()) {
            if (entry.getKey().equals("cnt")) {
                deviceCount = Integer.valueOf(String.valueOf(entry.getValue()));
            } else {
                deviceList.add(String.valueOf(entry.getValue()));
            }
        }

        if (deviceCount == deviceList.size()) {
            putJson = CommonTool.updateListSend(deviceList, (String) session.getAttribute("username"));
        } else {
            putJson.put("res", "cnt not equal");
        }

        return putJson.toString();
    }


    /*-----------------------------PAGE-----------------------------------------------------*/
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    protected String index(HttpServletRequest request) throws ParseException, IOException {
        return "/page/report_page";
    }

}
